#!/usr/bin/env python

# File:					add-to-dropbox
# Created:				November 2009
# Created by:			Axel von Bertoldi
# Last Modified:		November 2008
# Last Modified by:		Axel von Bertoldi
# (C) 2008				Axel von Bertoldi
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to:
# The Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor
# Boston, MA 02110-1301, USA.

import os
import sys
import re
from optparse import OptionParser

dropbox_path = os.path.expanduser ('~/Dropbox/home')
home = os.path.expanduser('~')
restore = False

if (len (sys.argv) < 2 or (len (sys.argv) < 3) and sys.argv[1].lower() == '-r'):
	print '### add-to-dropbox 0.1.1 ###'
	print 'usage: <add-to-dropbox> [options] files'
	print 'options:'
	print '  -r		Remove file from Dropbox and restore to its original location.\n'

for l in sys.argv[1:] :
	if (l.lower() == '-r'):
		restore = True;
		continue

	path = (os.path.abspath (l))
	if (os.path.exists (path)):
		basename = os.path.basename (path)
		prefix = os.path.dirname (path)
		path_less_home = re.sub (home, '', path)
		new_path = dropbox_path + path_less_home 

		if (restore):
			if (os.path.exists (new_path)):
				os.remove(path)
				os.renames (new_path, path)
				print ('"' + path + '" restored.')
			else:
				print ('"' + path + '" not in Dropbox.')
		else:
			if (os.path.exists (new_path)):
				print ('"' + path + '" already in Dropbox.')
			else:
				os.renames (path, new_path)
				os.symlink (new_path, path)
				print ('"' + path + '" added to Dropbox.')
	else:
		print ('Error: "' + path + '" does not exists.')
